package com.share.lottery.base;

import com.share.lottery.R;
import com.share.lottery.view.CustomToast;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;


/**
 * 基类
 * 
 * @author py
 * 
 */
public abstract class BaseActivity extends Activity implements OnClickListener {
	private ProgressDialog pDialog;
	private ProgressDialog apDialog;


	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		IntentFilter exitFilter = new IntentFilter();
		exitFilter.addAction("finish");
		registerReceiver(mFinishReceiver, exitFilter);
	}

	public void init(){
		initTitle();
		findView();
		getData();
		setData();
		setonCliker();
	};

	public abstract void findView();
	public abstract void getData();
	public abstract void initTitle();
	public abstract void setData();
	public abstract void setonCliker();
	/**
	 * 销毁当前界面
	 */
	public void onFinish() {
		this.finish();
	}

	/**
	 * 跳转界面
	 * 
	 * @param classz
	 *            跳转的界面
	 * @param isFinish
	 *            是否销毁当前界面 true:销毁
	 */
	public void turnToActivity(Class<?> classz, boolean isFinish) {
		startActivity(new Intent(this, classz));

		if (isFinish) {
			this.finish();
		}
		overridePendingTransition(R.anim.in_from_right, R.anim.out_to_right);
		// this.overridePendingTransition(R.anim.in_from_right,0);

	}

	/**
	 * 跳转界面
	 * 
	 * @param classz
	 *            跳转的界面
	 *  isFinish
	 *            是否销毁当前界面 true:销毁
	 * @param 通过bundle传递数据，获取哦的时候通过  intent.getBundleExtra("bundle");
	 */
	public void turnToActivity(Class<?> classz,Bundle bunlde,boolean isFinish) {

		Intent intent=new Intent(this, classz);
		intent.putExtra("bundle", bunlde);
		startActivity(intent);

		if (isFinish) {
			this.finish();
		}
		overridePendingTransition(R.anim.in_from_right, R.anim.out_to_right);

	}

	/**
	 * 销毁当前界面 直接跳转
	 * 
	 * @param classz
	 */

	public void showProgreessDialog() {

		if (pDialog == null) {
			pDialog = new ProgressDialog(this);
		}
		pDialog.setMessage("数据加载中..");
		pDialog.show();
	}

	/**
	 * 显示等待对话框 当点击返回键取消对话框并停留在该界面
	 * 
	 * @param hint
	 *            要显示的文字
	 */
	public void showProgreessDialog(String hint) {
		if (pDialog == null) {
			pDialog = new ProgressDialog(this);
		}

		pDialog.setMessage(hint);
		pDialog.show();
		pDialog.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					try {
						dismissProgressDialog();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				return false;
			}
		});
	}

	public void showmyProgreessDialog(String hint) {
		if (apDialog == null) {
			apDialog = new ProgressDialog(this);
		}
		apDialog.setCanceledOnTouchOutside(false);
		apDialog.setMessage(hint);
		apDialog.show();
		apDialog.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					try {
						dismissProgressDialog();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				return false;
			}
		});
	}

	public void dismissmyProgressDialog() {
		if (apDialog != null) {
			apDialog.dismiss();
		}
	}

	/**
	 * 销毁对话框
	 */
	public void dismissProgressDialog() {
		if (pDialog != null) {
			pDialog.dismiss();
		}
	}

	/**
	 * 弹一个短的土司
	 * 
	 * @param str
	 */
	public void showShortToast(String str) {
		CustomToast.makeText(this, str, 0);
	}

	/**
	 * 弹一个长的土司
	 * 
	 * @param str
	 */
	public void showLongToast(String str) {
		CustomToast.makeText(this, str, 1);
	}

	@Override
	public void onClick(View v) {

	}

	@Override
	protected void onResume() {
		super.onResume();
		// 页面开始
		//		StatService.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		// 页面结束
		//		StatService.onPause(this);
	}

	public BroadcastReceiver mFinishReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if ("finish".equals(intent.getAction())) {
				finish();
			}
		}
	};

	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mFinishReceiver);

	};


	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		this.finish();

	}


	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		overridePendingTransition(R.anim.in_from_right,R.anim.out_to_right);
	}
}
